﻿using ExcelMapper.Map.Interface;
using OfficeOpenXml;
using System.Collections.Generic;
using System.IO;
using ExcelMapper.Map.Utils;

namespace ExcelMapper
{
   
   public class ExcelMap : IMapper
    {
       private  int RowHeader { get; set; }
       private  int PreRowHeader { get; set; }
       private  int SheetPage { get; set; } 
       private  string Separator { get; set; }
       private string PathFile { get; set; }

       public ExcelMap()
       {

       }

       public ExcelMap(int rowHeader, int preRowHeader)
       {
           this.RowHeader = rowHeader;
           this.PreRowHeader = preRowHeader;
           this.SheetPage = 1;
       }

       public ExcelMap(int rowHeader, int preRowHeader, int sheetPage)
       {
           this.RowHeader = rowHeader;
           this.PreRowHeader = preRowHeader;
           this.SheetPage = sheetPage;
       }

       public ExcelMap(string path, int rowHeader, int preRowHeader, int sheetPage)
       {
           this.PathFile = path;
           this.RowHeader = rowHeader;
           this.PreRowHeader = preRowHeader;
           this.SheetPage = sheetPage;
       }

        public List<IDictionary<string, object>> LoadExcel(MemoryStream file, int rowHeader, int preRowHeader, int sheetPageNumber = 1)
        {
            List<IDictionary<string, object>> fileValues = new List<IDictionary<string, object>>();

            using (ExcelPackage package = new ExcelPackage(file))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets[sheetPageNumber];
                int maxColumns = worksheet.Dimension.End.Column;
                int maxRows = worksheet.Dimension.End.Row;

                string preHeaderName = string.Empty;

                for (int i = rowHeader + 1; i <= maxRows; i++)
                {
                    preHeaderName = string.Empty;
                    Dictionary<string, object> employeeExcel = new Dictionary<string, object>();

                    preHeaderName = GetValuesExcel(rowHeader, preRowHeader, worksheet, maxColumns, preHeaderName, i, employeeExcel);
                    fileValues.Add(employeeExcel);
                }

            }
            return fileValues;
        }

        public List<IDictionary<string, object>> LoadExcel(string filePath, int rowHeader, int preRowHeader, int sheetPageNumber = 1)
        {
            var transform = new  ConvertExcel();
            var file = transform.FromFile(filePath);

            return  LoadExcel(file, rowHeader,  preRowHeader,  sheetPageNumber);
        }

       public List<IDictionary<string, object>> LoadExcel()
        {
            return LoadExcel(this.PathFile, this.RowHeader, this.PreRowHeader, this.SheetPage);
        }

        private string GetValuesExcel(int rowHeader, int preRowHeader, ExcelWorksheet worksheet, int maxColumns, string preHeaderName, int i, Dictionary<string, object> employeeExcel)
        {
            for (int x = 1; x <= maxColumns; x++)
            {
                #region PreHeader
                if (preRowHeader > 0)
                {
                    string preHeaderNameTemp = ReturnValue(worksheet, x, preRowHeader);
                    if (!string.IsNullOrEmpty(preHeaderNameTemp))
                    {
                        preHeaderName = preHeaderNameTemp;
                    }
                }

                #endregion

                #region Header

                string headerName = ReturnValue(worksheet, x, rowHeader);

                if (!string.IsNullOrEmpty(preHeaderName))
                {
                    headerName = preHeaderName + "-" + headerName;
                }

                if (employeeExcel.ContainsKey(headerName))
                {
                    headerName = headerName + "-" + x;
                }

                #endregion

                #region value

                string value = ReturnValue(worksheet, x, i);

                #endregion


                employeeExcel[headerName] = value;

            }
            return preHeaderName;
        }

        private string ReturnValue(ExcelWorksheet sheet, int column, int row)
        {
            if (sheet.Cells[row, column].Value != null)
            {
                return sheet.Cells[row, column].Value.ToString();
            }
            else
            {
                return string.Empty;
            }

        }


        
    }
}
